package com.ohs.fo.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import com.ohs.bll.dto.SearchResultDTO;
import com.ohs.bll.service.AccountService;
import com.ohs.bll.service.BookingService;
import com.ohs.bll.service.LocationService;
import com.ohs.bll.service.RoomService;
import com.ohs.bll.service.ScheduleService;
import com.ohs.bll.util.CommonUtil;
import com.ohs.bll.util.ConfigurationManager;
import com.ohs.bll.util.Constant;
import com.ohs.bll.util.DateUtil;
import com.ohs.dal.model.Account;
import com.ohs.dal.model.Booking;
import com.ohs.dal.model.Room;
import com.ohs.dal.model.ScheduleId;

public class BookingAction extends AbstractAction {

	private RoomService roomService;
	
	private AccountService accountService;
	
	private LocationService locationService;
	
	private ScheduleService scheduleService;
	
	private BookingService bookingService;

	private Long locationId;
	
	private SearchResultDTO location;

	private List<Room> rooms;
	
	private String username;
	
	private String accountName;
	
	private String captcha;
	
	private String selectedDate;
	
	private String selectedTime;
	
	private String name;
	
	private String description;
	
	private String smsTo;
	
	private String smsKey;
	
	private String phone;
	
	private String age;
	
	private String address;
	
	private boolean recheck;
	
	private boolean allowBookRightaway;
	
	private boolean bookingAvailable;
	
	private Integer comeBefore;
	
	private long roomId;
	
	private LinkedHashMap<String, Object> jsonData = new LinkedHashMap<String, Object>();

	public String show() throws Exception {
		if(username != null && !"".equals(username)) {
			bookingAvailable = loadDetails();
			Account account = accountService.get(username);
			if (account == null || account.getStep() != Constant.STEP_DONE_3) {
				bookingAvailable = false;
			}
		} else {
			location = locationService.getById(locationId);
			if (location.getUsername() != null && !"".equals(location.getUsername())) {
				username = location.getUsername();
				bookingAvailable = loadDetails();
				Account account = accountService.get(username);
				if (account == null || account.getStep() != Constant.STEP_DONE_3) {
					bookingAvailable = false;
				}
			}
		}
		return "success";
	}

	private boolean loadDetails() {
		if (username == null) {
			username = accountName;
		}
		rooms = new ArrayList<Room>();
		location = locationService.getFullDetail(username);
		List<Room> tmpRooms = roomService.findByLocation(location.getLocationId());
		if (tmpRooms == null || tmpRooms.size() == 0) {
			return false;
		}
		boolean hasAvailableRoom = false;
		for (Room room : tmpRooms) {
			if (room.getTurnOn()) {
				hasAvailableRoom = true;
				rooms.add(room);
			}
		}
		if (!hasAvailableRoom) {
			return false;
		}
		smsTo = ConfigurationManager.getAsString(ConfigurationManager.CONFIG, "smsTo");
		smsKey = ConfigurationManager.getAsString(ConfigurationManager.CONFIG, "smsKey");
		return true;
	}
	
	public String getTime() throws Exception {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
	    cal.setTime(sdf.parse(selectedDate));
		int val = cal.get(Calendar.DAY_OF_WEEK);
		List<String> timeSlots = scheduleService.getAvailableTime(username, new ScheduleId(roomId, val), selectedDate);
		jsonData.put("timeSlots", timeSlots);
		return "success";
	}

	public String book() throws Exception {
		clearMessages();
		if (CommonUtil.checkSMSCaptcha(phone, captcha)) {
			Date bookingDate = DateUtil.stringToDate(selectedDate, selectedTime);
			List<Booking> existedBooking = bookingService.findByDate(bookingDate);
			if (existedBooking != null && existedBooking.size() > 0) {
				addActionError("Đã có người đặt hẹn vào giờ này trước bạn, vui lòng chọn giờ khác");
				bookingAvailable = loadDetails();
				return "existed";
			}
			Room room = roomService.get(roomId);
			Booking booking = new Booking();
			booking.setName(name);
			booking.setPhone(phone);
			booking.setAge(Integer.valueOf(age));
			booking.setDescription(description);
			booking.setAddress(address);
			booking.setRecheck(recheck);
			booking.setCaptcha(captcha);
			booking.setBookingDate(bookingDate);
			booking.setRoom(room);
			booking.setCreatedDate(new Date());
			bookingService.saveOrUpdate(booking);
			
			location = locationService.getFullDetail(username);
			CommonUtil.removeCaptcha(phone);
			comeBefore = locationService.getByAccount(username).getComeBefore();
			return "success";
		} else {
			addActionMessage("Mã xác nhận không đúng");
			addActionError("Mã xác nhận không đúng");
			bookingAvailable = loadDetails();
			return "input";
		}
	}
	
	public RoomService getRoomService() {
		return roomService;
	}

	public String getCaptcha() {
		return captcha;
	}

	public ScheduleService getScheduleService() {
		return scheduleService;
	}

	public void setScheduleService(ScheduleService scheduleService) {
		this.scheduleService = scheduleService;
	}

	public long getRoomId() {
		return roomId;
	}

	public boolean isAllowBookRightaway() {
		return allowBookRightaway;
	}

	public void setAllowBookRightaway(boolean allowBookRightaway) {
		this.allowBookRightaway = allowBookRightaway;
	}

	public String getAccountName() {
		return accountName;
	}

	public void setAccountName(String accountName) {
		this.accountName = accountName;
	}

	public boolean isBookingAvailable() {
		return bookingAvailable;
	}

	public void setBookingAvailable(boolean bookingAvailable) {
		this.bookingAvailable = bookingAvailable;
	}

	public AccountService getAccountService() {
		return accountService;
	}

	public void setAccountService(AccountService accountService) {
		this.accountService = accountService;
	}

	public void setRoomId(long roomId) {
		this.roomId = roomId;
	}

	public Integer getComeBefore() {
		return comeBefore;
	}

	public void setComeBefore(Integer comeBefore) {
		this.comeBefore = comeBefore;
	}

	public LinkedHashMap<String, Object> getJsonData() {
		return jsonData;
	}

	public void setJsonData(LinkedHashMap<String, Object> jsonData) {
		this.jsonData = jsonData;
	}

	public SearchResultDTO getLocation() {
		return location;
	}

	public void setLocation(SearchResultDTO location) {
		this.location = location;
	}

	public void setCaptcha(String captcha) {
		this.captcha = captcha;
	}

	public String getSmsTo() {
		return smsTo;
	}

	public void setSmsTo(String smsTo) {
		this.smsTo = smsTo;
	}

	public String getSmsKey() {
		return smsKey;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public boolean isRecheck() {
		return recheck;
	}

	public void setRecheck(boolean recheck) {
		this.recheck = recheck;
	}

	public void setSmsKey(String smsKey) {
		this.smsKey = smsKey;
	}

	public void setRoomService(RoomService roomService) {
		this.roomService = roomService;
	}

	public Long getLocationId() {
		return locationId;
	}

	public void setLocationId(Long locationId) {
		this.locationId = locationId;
	}

	public String getName() {
		return name;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getAge() {
		return age;
	}

	public void setAge(String age) {
		this.age = age;
	}

	public BookingService getBookingService() {
		return bookingService;
	}

	public void setBookingService(BookingService bookingService) {
		this.bookingService = bookingService;
	}

	public LocationService getLocationService() {
		return locationService;
	}

	public void setLocationService(LocationService locationService) {
		this.locationService = locationService;
	}

	public List<Room> getRooms() {
		return rooms;
	}

	public void setRooms(List<Room> rooms) {
		this.rooms = rooms;
	}

	public String getSelectedDate() {
		return selectedDate;
	}

	public void setSelectedDate(String selectedDate) {
		this.selectedDate = selectedDate;
	}

	public String getSelectedTime() {
		return selectedTime;
	}

	public void setSelectedTime(String selectedTime) {
		this.selectedTime = selectedTime;
	}


}